<?php
/**
 * Created by PhpStorm.
 * User: yangbangcheng
 * Date: 2017/12/28
 * Time: 11:34
 */

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
    //列表
    public function index(){
        return view("customer/index");
    }
    //详情页面
    public function show(Post $post){
        $post->load('comments');
        return view("post/show", compact('post'));
    }
    //创建页面
    public function create(){
        return view("post/create");
    }
    //创建逻辑
    public function store(){

       // dd(\Request::all());
       // $post=new Post();
       /// $post->title=request('title');
        //$post->content=request('title');
       // $post->save();
        //$params=['title'=>request('title'),'content'=>request('content')];
        // dd($post); 输出数据
        // dd(request()->all());//打印数据时使用dd
        //return view("post/store");
        //$this->validate(request(),
            //['title'=>'required|string|max:100|min:5',
             // 'content'=>'required|string|min:5'
              //  ],[
              //   'title.min'=>'文章标题过短'
           // ]//自定义
       // );
        //验证
        $this->validate(request(), [
                'title'=>'required|string|max:100|min:5',
                'content'=>'required|string|min:5'
            ]);
        //逻辑
        $user_id=\Auth::ID();
        $params=array_merge(request(['title','content']),compact('user_id'));
       $post=Post::create($params); //创建字段
        //渲染
        return redirect("/posts");

    }
    //编辑页面
    public function edit(Post $post){
        return view("post/edit",compact('post'));
    }
    //编辑逻辑
    public function update(Post $post){

       $this->validate(request(), [
            'title'=>'required|string|max:100|min:5',
            'content'=>'required|string|min:5'
        ]);

        $this->authorize('update','$post');

        $post->title=request('title'); //创建字段
        $post->content=request('content');
        $post->save();

        return redirect("/posts/{$post->id}");///
    }
    //删除逻辑
    public function delete(Post $post){
        //用户权限验证

        $this->authorize('delete','$post');

        $post->delete();
        return redirect("/posts");
    }
    public function imageUpload(Request $request)
    {
        //echo "aaaa";die;
        //dd(request()->all());die;

        $path=$request->file('wangEditorH5File')->storePublicly(md5(time()));

        return asset('storage/'.$path);

    }
    //评论
    public  function comment(Post $post){
        $this->validate(request(),['content'=>'required|min:3',

        ]);
        //逻辑
        $comment=new Comment();
        $comment->user_id=\Auth::id();
        $comment->content=request('content');
        $post->comments()->save($comment);
        //渲染
        return  back();
    }
    //赞
    public function zan(Post $post){
        $param=[
            'user_id'=>\Auth::id(),
            'post_id'=>$post->id,
        ];
        Zan::firstOrCreate($param);//如果从模型种找不到就创建数据
        return back();
    }
    //取消赞
    public function unzan(Post $post){
        $post->zan(\Auth::id())->delete();
        return back();
    }
    //搜索结果页
    public function search(){
        //验证
            $this->validate(request(),['query'=>'required']);
        //逻辑
            $query=request('query');
            $posts=\App\Post::search($query)->paginate(2);
        //渲染
        return view("post/search",compact('posts','query'));
    }


}